<?php

namespace App\Http\Controllers\Guard\Advert;

use App\Http\Controllers\Common\GuardController;
use App\Models\Constant\Constant;
use App\Models\Constant\UploadConstant;
use App\Models\Setting\Upload;
use Illuminate\Http\Request;

class AdvertController extends GuardController {

    public function lst(Request $request) {

        $type = $request->input('type','all');
        $status = $request->input('status');
        $pageSize = $request->input('pageSize',Constant::PAGE_SIZE);
        $page  = $request->input('page',1);
        $conditions = compact('type','status');
        $records = (new Upload())->getLst($conditions)->paginate($pageSize,$this->getCol,'page',$page);

        foreach ($records as $record) {
            $record->type = UploadConstant::MAP_TYPE[$record->type]??'未知';;
        }

        $mapType = [];
        foreach (UploadConstant::MAP_TYPE as $value=>$label) {
            $mapType[] = compact('value','label');
        }
        return responseSuccess($records->items(),$records->total(),100000,['map_type'=>$mapType]);
    }

    public function add(Request $request) {

        $id = $request->input('id',0);
        $info = $request->input();
        /* $pic_url = $request->input('picUrl');
        $type = $request->input('type');
        $title = $request->input('title');
        $link = $request->input('link');
        $status = $request->has('status')?1:2; */
        Upload::updateOrCreate(compact('id'),$info);
        return responseSuccess();
    }

    public function getRecord(Request $request) {
        return $this->getRecordByIds(Upload::class, $request->input('id','')); // TODO: Change the autogenerated stub
    }

    public function del(Request $request) {
        return $this->delRecords(Upload::class, $request->input('id',''));
    }

}
